<?php echo $this->render('/public/header'); ?>
    <link href="https://cdn.bootcss.com/element-ui/2.0.7/theme-chalk/index.css" rel="stylesheet">
    <script src="https://cdn.bootcss.com/element-ui/2.0.7/index.js"></script>
    <style>
        .jing_input {
            width: 80%;
            border: none;
            margin: 0px;
            padding: 0px
        }

        .jing_input hover {
            border: 1px solid #50A7FF;
        }
    </style>
    <div class="col-lg-12 col-xs-12 col-md-12 col-sm-12" id="main">
        <a href="javascript:window.history.go(-1)" class="btn btn-success" style="float:left">返回</a>
        <br/>
        <br/>
        <br/>
        <div class="col-lg-12 col-md-12 col-sm-12 col-md-12">
            <div class="col-lg-4 col-md-4 col-xs-4 col-sm-4">
                <ul class="list-group">
                    <?php foreach ($data['list'] as $v) : ?>
                        <li class="list-group-item">
                            <?php echo $v['date']; ?>-
                            <?php echo $v['product']; ?>- 转出:
                            <?php echo $v['out_money']; ?> 转入:
                            <?php echo $v['in_money']; ?>-
                            <?php echo $v['name']; ?>
                        </li>
                    <?php endforeach; ?>

                    <?php foreach ($data['express_list'] as $v): ?>
                        <li class="list-group-item">
                            <?php foreach ($expressType as $v2): ?>
                                <?php if ($v2['id'] == $v['express_id']): ?>
                                    <?php echo $v2['name'] . '运费--' . $v['fee']; ?>- 转出:
                                <?php endif; ?>
                            <?php endforeach; ?>
                        </li>
                    <?php endforeach; ?>

                    <?php foreach ($data['repo_info'] as $v) : ?>
                        <li class="list-group-item">
                            <?php echo $v['date']; ?>-
                            <?php echo '仓库费' . $v['product']; ?>- 转出:
                            <?php echo $v['pay_all']; ?>
                        </li>
                    <?php endforeach; ?>

                    <?php foreach ($data['print_info'] as $v) : ?>
                        <li class="list-group-item">
                            <?php echo $v['date']; ?>-
                            <?php echo '印刷费' . $v['product']; ?>- 转出:
                            <?php echo $v['fee']; ?>
                        </li>
                    <?php endforeach; ?>

                    <?php foreach ($data['inland_info'] as $v) : ?>
                        <li class="list-group-item">
                            <?php echo $v['date']; ?>-
                            <?php echo '内陆费' . $v['remark']; ?>- 转出:
                            <?php echo $v['total_fee']; ?>
                        </li>
                    <?php endforeach; ?>
                    <?php foreach ($data['baoxiao'] as $k => $v) : ?>
                        <li class="list-group-item">
                            <?php echo $k; ?>
                            <?php echo '报销'; ?>- 转出:
                            <?php echo $v; ?>
                        </li>
                    <?php endforeach; ?>
                    <hr>
                    <?php foreach ($productList as $v): ?>
                        <li class="list-group-item">
                            <?php echo $v['item']; ?>
                            <?php echo '采购量：' . $v['num']; ?>
                            <?php echo '采购单价:' . $v['price']; ?>
                        </li>
                    <?php endforeach; ?>
                </ul>
            </div>
            <div class="col-lg-8 col-sm-8  col-md-8 ">
                <button onclick="Page.add()" class="btn btn-success">添加项目</button>
                <button class="btn btn-danger" onclick="resetProduct()">重新生成产品</button>
                <button class="btn btn-success" style="float:right;" onclick="download()">下载excel</button>
                <br/>
                <br/>
                <div class="col-lg-3 col-xs-3">客户:<b><?php echo $data['info']['customer_name']; ?></b></div>
                <div class="col-lg-3 col-xs-3">国家:<b><?php echo $data['info']['country']; ?></b></div>
                <div class="col-lg-3 col-xs-3">invoice:<b><?php echo $data['info']['invoice']; ?></b></div>
                <table class="table table-borderd">
                    <thead>
                    <tr>
                        <th>日期</th>
                        <th>编号</th>
                        <th>名称</th>
                        <th>采购单价</th>
                        <th>数量</th>
                        <th>额外费用</th>
                        <th>额外费用备注</th>
                        <th>金额</th>
                        <th>Name</th>
                        <th>Price</th>
                        <th>Extra Fee</th>
                        <th>Subtotal</th>
                        <th>利润</th>
                        <th>利润比</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    <?php $rmb_pay = 0;
                    $dollar_income = 0 ?>
                    <?php foreach ($data['product'] as $k => $v) : ?>
                        <?php $rmb_pay += ($v['price_rmb'] * $v['number'] + $v['extra_fee']); ?>
                        <?php $dollar_income += ($v['price_dollar'] * $v['number'] + $v['customer_extra_fee']); ?>
                        <tr>
                            <td></td>
                            <td>
                                <p style="text-align: center"><?php echo $k + 1; ?></p>
                            </td>
                            <?php if ($data['edit']) {
                                ; ?>
                                <td><input onchange="update(<?php echo $v['id']; ?>,'product_name',this)"
                                           class="jing_input"
                                           type="text" value="<?php echo $v['product_name']; ?>"/></td>
                                <td>￥<input onchange="update(<?php echo $v['id']; ?>,'price_rmb',this)"
                                            class="jing_input"
                                            type="text" value="<?php echo $v['price_rmb']; ?>"/></td>
                                <td><input onchange="update(<?php echo $v['id']; ?>,'number',this)" class="jing_input"
                                           type="text" value="<?php echo $v['number']; ?>"/></td>
                                <td>￥<input onchange="update(<?php echo $v['id']; ?>,'extra_fee',this)"
                                            class="jing_input"
                                            type="text" value="<?php echo $v['extra_fee']; ?>"/></td>
                                <td><input onchange="update(<?php echo $v['id']; ?>,'remark',this)" class="jing_input"
                                           type="text" value="<?php echo $v['remark']; ?>"/></td>
                                <td>
                                    ￥<?php echo $rmb = $v['price_rmb'] * $v['number'] + $v['extra_fee']; ?>
                                </td>
                                <td><input class="jing_input"
                                           onchange="update(<?php echo $v['id']; ?>,'product_name_en',this)" type="text"
                                           value="<?php echo $v['product_name_en']; ?>"/></td>
                                <td>$ <input onchange="update(<?php echo $v['id']; ?>,'price_dollar',this)" type="text"
                                             class="jing_input" value="<?php echo $v['price_dollar']; ?>"/></td>
                                <td>$ <input onchange="update(<?php echo $v['id']; ?>,'customer_extra_fee',this)"
                                             type="text" class="jing_input"
                                             value="<?php echo $v['customer_extra_fee']; ?>"/></td>
                            <?php } else { ?>
                                <td><?php echo $v['product_name']; ?></td>
                                <td><?php echo $v['price_rmb']; ?></td>
                                <td><?php echo $v['number']; ?></td>
                                <td><?php echo $v['extra_fee']; ?></td>
                                <td><?php echo $v['remark']; ?></td>
                                <td>
                                    ￥<?php echo $rmb = $v['price_rmb'] * $v['number'] + $v['extra_fee']; ?>
                                </td>
                                <td><?php echo $v['product_name_en']; ?></td>
                                <td>$<?php echo $v['price_dollar']; ?></td>
                                <td>$<?php echo $v['customer_extra_fee']; ?></td>
                            <?php } ?>
                            <td>$
                                <?php echo $total = ($v['price_dollar'] * $v['number'] + $v['customer_extra_fee']); ?>
                            </td>
                            <td>￥
                                <?php $lilun = $total * (yii::$app->request->get('rate')) - $rmb;
                                echo round($lilun, 2) ?>
                            </td>
                            <td>
                                <?php echo $total ? round($lilun / ($total * (yii::$app->request->get('rate'))) * 100, 2) : 0; ?>
                                %
                            </td>
                            <td>
                                <button style="width:100%" onclick="del(<?php echo $v['id']; ?>)"
                                        class="btn btn-success">删除
                                </button>
                            </td>
                        </tr>
                    <?php endforeach; ?>

                    </tbody>
                </table>
                <hr/>
                <!--                --><?php //foreach ($this->params['repo_info'] as $v) : ?>
                <!--                    <li class="list-group-item">-->
                <!--                        --><?php //echo $v['date']; ?><!----->
                <!--                        --><?php //echo '仓库费' . $v['product']; ?><!--- 转出:-->
                <!--                        --><?php //echo $v['pay_all']; ?>
                <!--                    </li>-->
                <!--                --><?php //endforeach; ?>
                <!---->
                <!--                --><?php //foreach ($this->params['print_info'] as $v) : ?>
                <!--                    <li class="list-group-item">-->
                <!--                        --><?php //echo $v['date']; ?><!----->
                <!--                        --><?php //echo '印刷费' . $v['product']; ?><!--- 转出:-->
                <!--                        --><?php //echo $v['fee']; ?>
                <!--                    </li>-->
                <!--                --><?php //endforeach; ?>
                <!---->
                <!--                --><?php //foreach ($this->params['inland_info'] as $v) : ?>
                <!--                    <li class="list-group-item">-->
                <!--                        --><?php //echo $v['date']; ?><!----->
                <!--                        --><?php //echo '内陆费' . $v['remark']; ?><!--- 转出:-->
                <!--                        --><?php //echo $v['total_fee']; ?>
                <!--                    </li>-->
                <!--                --><?php //endforeach; ?>
                <?php function get_pay_type($type)
                {
                    if ($type == 1) {
                        return 'paypal';
                    } elseif ($type == 2) {
                        return 'T/T';
                    } elseif ($type == 3) {
                        return '西联';
                    }
                } ?>
                <li class="list-group-item">
                    <div class="form-inline" id="detail">
                        <div class="form-group" style="width:12%">
                            收款详情:<br/>
                            <?php $real_income = 0;
                            foreach ($data['income_list'] as $v) : ?>
                                <?php $tmp = $v['in_come'] + $v['total_fee'] + $v['fee'];
                                $real_income += $tmp;
                                echo $v['date'] . '   ' . (get_pay_type($v['pay_from'])) . '  收款   $' . $tmp; ?>
                                <br/>
                            <?php endforeach; ?>
                        </div>
                        <div class="form-group" style="width:12%">
                            出货详情:<br/>
                            <?php if ($data['edit']) { ?>
                                <el-input type="textarea" autosize v-model="remark" @change="change"></el-input>
                            <?php } else { ?>
                                {{remark}}
                            <?php } ?>

                        </div>
                        <div class="form-group" style="width:10%">
                            总支出<br/>
                            <?php echo $rmb_pay; ?>
                        </div>
                        <div class="form-group" style="width:10%">
                            汇率<br/>
                            <?php echo $rate = yii::$app->request->get('rate') ?? 0; ?>
                        </div>
                        <div class="form-group" style="width:10%">
                            总金额<br/>
                            $<?php echo $dollar_income; ?>
                        </div>
                        <div class="form-group" style="width:10%">
                            总收入<br/>
                            $<?php echo $real_income; ?>
                        </div>
                        <div class="form-group" style="width:10%">
                            差额<br/>
                            $<?php echo round(($real_income - $dollar_income), 2); ?>
                        </div>
                        <div class="form-group" style="width:10%">
                            利润<br/>
                            <?php echo round($real_income * $rate - $rmb_pay, 2); ?>
                        </div>
                        <div class="form-group" style="width:12%">
                            利润比<br/>
                            <?php echo $real_income ? round(($real_income * $rate - $rmb_pay) / ($real_income * $rate) * 100, 2) . '%' : 0; ?>
                        </div>
                    </div>
                </li>
            </div>
        </div>
    </div>
    <script>
        new Vue({
            el: "#detail",
            data: {
                remark: `<?php echo $data['detail']['detail'];?>`
            },
            methods: {
                change: function (v) {
                    this.$confirm('确认上传').then(() => {
                        $.post('/table/update-order', {
                            invoice: '<?php echo yii::$app->request->get('invoice');?>',
                            param: "detail",
                            value: v
                        }, (res) => {
                            if (res.code == 1) {
                                this.$message({
                                    message: res.msg,
                                    type: 'success'
                                });
                            } else {
                                this.$message.error(res.msg);
                            }
                        }, 'json')
                    })
                }
            }
        });
        var Page = {};
        Page.add = function () {
            zeroModal.show({
                width: '1000px',
                url: "/api/add_product?invoice=<?php echo yii::$app->request->get('invoice');?>"
            });
        }

        function download() {
            var url = '/download/download-order-detail' + window.location.search;
            window.location.href = url;
        }

        function resetProduct() {
            let invoice = '<?php echo yii::$app->request->get('invoice');?>';
            layer.confirm('确认重新生成，此操作将会删除原有数据', {}, function () {
                $.post('/table/generate-product', {in: invoice}, function (res) {
                    if (res.code == 1) {
                        layer.alert(res.msg);
                        setTimeout(function () {
                            window.location.reload();
                        }, 1000)
                    } else {
                        layer.alert(res.msg);
                    }
                }, 'json')
            })
        }

        function update(id, column, e) {
            layer.confirm('确认修改', {}, function () {
                var data = {};
                data.id = id;
                data.column = column;
                data.value = e.value;
                var url = '/purchase/purchase_update';
                $.post(url, data, function (res) {
                    if (res.code == 1) {
                        layer.confirm(res.msg, {}, function () {
                            window.location.reload();
                        })
                    } else {
                        layer.alert(res.msg);
                    }
                }, 'json');
            });
        }

        function del(id) {
            layer.alert('确认删除', {}, function () {
                $.post('/table/del-order-detail-record', {
                    id: id,
                    invoice: '<?php echo yii::$app->request->get('invoice');?>'
                }, function (res) {
                    layer.alert(res.msg);
                    if (res.code == 1) {
                        setTimeout(function () {
                            window.location.reload();
                        }, 1000)
                    }
                }, 'json')
            })
        }
    </script>
<?php echo $this->render('/public/footer'); ?>